﻿using BlondeHeaven.BLL.Interface;
using BlondeHeaven.DAL.Interface;
using BlondeHeaven.Model.Model;
using System;
using System.Collections.Generic;
using System.Text;

namespace BlondeHeaven.BLL
{
    public class BllCommodityService<T> : IBllCommodityService<T> where T : Commodity, new()
    {

        /// <summary>
        /// 数据库服务
        /// </summary>
        protected IDalCommodityService<T> _db;

        public BllCommodityService(IDalCommodityService<T> dalService)
        {
            _db = dalService;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="model"></param>
        /// <param name="IsSave"></param>
        /// <returns></returns>
        public T Add(T model, bool IsSave)
        {
            model = _db.Add(model);
            if (IsSave)
            {
                if (SaveChanges() > 0)
                    return null;
            }
            return model;
        }
        /// <summary>
        /// 修改店铺信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public T ChangeShopKeeperInformation(T model, bool IsSave)
        {
            _db.EditAsync(model);
            if (IsSave)
            {
                if (SaveChanges() > 0)
                    return null;
            }
            return model;
        }
        /// <summary>
        /// 查询所有商店
        /// </summary>
        public List<Commodity> GetAllCommodity(int id)
        {
            return _db.GetAllCommodity(id);
        }
        /// <summary>
        /// 保存道数据库
        /// </summary>
        /// <returns></returns>
        /// 
        public int SaveChanges()
        {
            return _db.SaveChanges();
        }
        public T RemoveAsync(T model, bool IsSave)
        {
            _db.RemoveAsync(model);
            if (IsSave)
            {
                if (SaveChanges() > 0)
                    return null;
            }
            return model;
        }

        public T GetByoneId(int id)
        {
            return _db.GetOneById(id);
        }
        public List<Commodity> GetByAllName(int id ,string Name)
        {
            return _db.GetAllByName(id,Name);
        }
    }
}
